from .io_operation import BasicIo
import threading


class Lighting:
    """
    照明操作
    """
    IDLE = 0
    # CAMMAND_SENDED = 1
    LIGHTING_CONTINUE = 2

    # # 单例
    # g_lighting = None
    # g_lighting_mutex = threading.Lock()
    #
    # @classmethod
    # def single_instance(cls):
    #     cls.g_lighting_mutex.acquire()
    #
    #     if cls.g_lighting is None:
    #         cls.g_lighting = Lighting()
    #
    #     cls.g_lighting_mutex.release()
    #
    #     return cls.g_lighting


    def __init__(self, channel_index=None):
        if channel_index is None:
            self.lighting_io = BasicIo("Lighting")
        else:
            self.lighting_io = BasicIo("Lighting_ch%d" % channel_index)
        self.current_step = self.IDLE

    def open(self):
        self.lighting_io.set_true()
        # self.current_step = self.CAMMAND_SENDED
        self.current_step = self.LIGHTING_CONTINUE

    def close(self):
        if self.current_step == self.IDLE:
            return

        self.lighting_io.set_false()
        self.current_step = self.IDLE

    def is_open(self):
        if self.current_step == self.IDLE:
            print("处于IDLE状态")
            return False

        return self.lighting_io.read_state()

        # g_lighting = None
        # g_lighting_mutex = threading.Lock()
        # def get_lighting_instance():
        #     global g_lighting
        #
        #     g_lighting_mutex.acquire()
        #
        #     if g_lighting is None:
        #         g_lighting = Lighting()
        #
        #     g_lighting_mutex.release()
        #
        #     return g_lighting
